package wh.coach._collection;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hangwen
 * @date 2023/6/28
 */
public class ZeroCopyList {

    public static void main(String[] args) {
        final List<Integer> arrayList = new ArrayList<>();

        //给定参数count,value
        int count = 100;
        Integer v = 1;

        for (int i = 0; i < count; i++) {
            arrayList.add(v);
        }

        for (Integer integer : arrayList) {
            System.out.println(integer);
        }

        final List<Integer> list = new RepeatValueList<>(100, 1);
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }

    static class RepeatValueList<T> extends AbstractList<T> {
        private final int count;
        private final T v;

        RepeatValueList(final int count, final T v) {
            this.count = count;
            this.v = v;
        }

        @Override
        public T get(final int index) {
            return v;
        }

        @Override
        public int size() {
            return count;
        }
    }
}
